package ch.unisi.inf.viewer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import ch.unisi.inf.engine.Image;
import ch.unisi.inf.gui.MainFrame;

import javax.swing.JPanel;


/**
 * 
 * @author matthias.eberli@lu.unisi.ch
 *
 */
public class MainImgViewerPanel extends JPanel{
	private final ArrayList<Image> imageList;
	private final JPanel imagesPanel;
	private final ArrayList<MouseListener> listeners;
	private final MainFrame mainFrame;
	private ArrayList<ThumbnailFactory> thumbnailsList;
	private Image selectedImage;
	
	public MainImgViewerPanel(final ArrayList<Image> imageList, final MainFrame mainFrame){
		this.mainFrame = mainFrame;
		this.imageList = imageList;
		this.setLayout(new BorderLayout());
		thumbnailsList = new ArrayList<ThumbnailFactory>();
		listeners = new ArrayList<MouseListener>();
		
		imagesPanel = new JPanel();
		imagesPanel.setLayout(new FlowLayout(0, 10, 10));
		imagesPanel.setPreferredSize(new Dimension(600,400));
		
		for(final Image image : imageList){
			final ThumbnailFactory thumbnail = new ThumbnailFactory(image);
			thumbnailsList.add(thumbnail);
			
		}
		
		for(final ThumbnailFactory thumb : thumbnailsList)	{
			thumb.addMouseListener(new MouseAdapter() {
				public void mouseClicked (final MouseEvent ev) {
					mainFrame.setSelectedImage(thumb.getImage());
					
					for(final ThumbnailFactory thumbInt : thumbnailsList){
						thumbInt.setSelected(false);
						thumbInt.paintBorder();
					}
					
					thumb.setSelected(true);
					selectedImage = thumb.getImage();
					thumb.paintBorder();
				}
			});
			imagesPanel.add(thumb);
		}
	
		this.add(imagesPanel);
		setVisible(true);
	}
	
	public Image getSelectedImage(){
		return selectedImage;
	}
}
